package com.zcaxer.way.building;

public class Asset implements AssetCallback {

    private Building1 foodProducer = new Building1(Building1.BuildingType.Food, 1) {
        @Override
        protected void calculateData() {
            maxWorkers = 3 * level + 2;
            foodIncrease = 1;
            switch (level / 100) {
                case 0:
                    capacity = calculate(level, 270, 180, 0);
                    upWoodNeed = calculate(level, 81, 54, 0);
                    break;

                case 1:
                case 2:
                    capacity = calculate(level, 540, -26640, -18000);
                    upWoodNeed = calculate(level, 189, -9324, -6300);
                    if (level == 100)
                        upWoodNeed = calculate(level, 81, 54, 0);//100为case1但capacity满足公式而升级所需木材满足case0公式
                    break;
            }
        }
    };

    private Building1 woodProducer = new Building1(Building1.BuildingType.Wood, 1) {
        @Override
        protected void calculateData() {
            int a = 0;
            maxWorkers = 2 * level;
            foodIncrease = -2;
            capacity = calculate(level, 180, 0, 0);
            switch ((level - 1 + 50) / 50 - 1) {//避免出现0作被除数
                case 0:
                case 1: {
                    a = 90;
                    if (level == 100) a = 99;//100为case1但应使用case2公式
                    break;
                }
                case 2:
                    a = 99;
                    break;
                case 3:
                    a = 108;
                    break;
                case 4:
                    a = 117;
                    break;
                case 5:
                    a = 126;
                    break;
            }
            upWoodNeed = calculate(level, a, 0, 0);

        }
    };
    private Building1 grassProducer = new Building1(Building1.BuildingType.Grass, 1) {
        @Override
        protected void calculateData() {
            maxWorkers = level;
            foodIncrease = -4;
            capacity = calculate(level, 90, 0, 0);
            upWoodNeed = calculate(level, 54, 0, 0);
        }
    };
    private Building1 ironProducer = new Building1(Building1.BuildingType.Iron, 1) {
        @Override
        protected void calculateData() {
            maxWorkers = level;
            foodIncrease = -4;
            capacity = calculate(level, 90, 0, 0);
            upWoodNeed = calculate(level, 54, 0, 0);

        }
    };

    private Building1[] building1s = {foodProducer, woodProducer, grassProducer, ironProducer};
    private int food;
    private int wood;
    private int iron;
    private int grass;
    private float time;
    private int foodIncreaseSum = calculateFoodIncrease();

    @Override
    public void assetIncrease(Building1.BuildingType key, int signedIncreaseNumber) {


        switch (key) {
            case Food:
                food += signedIncreaseNumber;
            case Wood:
                wood += signedIncreaseNumber;
            case Grass:
                grass += signedIncreaseNumber;
            case Iron:
                iron += signedIncreaseNumber;
        }
    }

    public void product(float time) {
        if ((foodIncreaseSum + food) > 0) {
            food = (food + foodIncreaseSum < foodProducer.capacity) ? food + foodIncreaseSum : foodProducer.capacity;
            wood = (wood + woodProducer.workers < woodProducer.capacity) ? wood + woodProducer.workers : woodProducer.capacity;
            grass = (grass + grassProducer.workers < grassProducer.capacity) ? grass + grassProducer.workers : grassProducer.capacity;
            iron = (iron + ironProducer.workers < ironProducer.capacity) ? iron + ironProducer.workers : ironProducer.capacity;

        }
    }

    public int getFood() {
        return food;
    }

    public int getGrass() {
        return grass;
    }

    public int getIron() {
        return iron;
    }

    public int getWood() {
        return wood;
    }

    public Building1[] getBuilding1s() {
        return building1s;
    }

    private int calculateFoodIncrease() {
        int sum = 0;
        for (Building1 a : building1s) {
            sum = sum + a.workers * a.foodIncrease;
        }
        return sum;
    }


    private int calculate(int x, int a, int b, int c) {
        return x * x * a + x * b + c;
    }
}
